<?php

class MY_Controller extends CI_Controller {

    public $_site_path;
    public $layout = "layouts/main";
    private $checkIpFlag = false;

    public function __construct() {

        date_default_timezone_set('PRC');
        parent::__construct();
        $this->_site_path = config_item('site_path');
        $this->load->library('auth');

        if ($this->checkIpFlag) {
            $this->checkIp();
        }

        if (!$this->auth->hasLogin()) {
            redirect($this->_site_path . '/login');
        }
    }

    private function checkIp() {
        $this->load->library('check_ip');
        $this->load->model("config_model");
        $is_check_ip = $this->config_model->get_values("id", "key", array("status" => 1, "key" => "check_ip"));
        if (!empty($is_check_ip) && !($this->check_ip->checkIP())) {
            echo "无此权限,请联系管理员!error Ip";
            exit;
        }
    }

}

class Front_Controller extends CI_Controller {

    public $layout = "layouts/front";
    public $_site_path;
    public $pagination_config = array(
        'full_tag_open' => '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'cur_tag_open' => '<li class="active"><a href="#">',
        'cur_tag_close' => '</a></li>',
        'num_tag_open' => '<li>',
        'num_tag_close' => '</li>',
        'next_tag_open' => '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' => '<li>',
        'prev_tag_close' => '</li>',
        'first_tag_open' => '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' => '<li>',
        'last_tag_close' => '</li>',
        'anchor_class' => '',
    );

    public function __construct() {
        $this->_site_path = config_item('front_path');
        date_default_timezone_set('PRC');
        parent::__construct();
    }

    //获取头部和导航栏菜单
    public function get_banner() {
        $this->load->model('news_class_model');
        $search = array(
            'attributes' => array('parentid' => 0),
            'orders' => array('depth' => 'asc', 'created' => 'desc'),
        );
        $nav_data['nav_data'] = $this->news_class_model->all($search);
        //$datas = $this->news_class_model->all();
        /* foreach($datas as $v){
          $data['sortname']=$v['sortname'];
          $data['id']=$v['id'];
          switch($v['sortname']){
          case "产品介绍":
          $data['url']=base_url($this->_site_path . "/news/goods_list/$v[id]");
          break;

          default:
          break;
          }
          } */
        $this->load->view_part($this->_site_path . '/common/header', $nav_data);
    }

    //获取头部和导航栏菜单
    public function headerInfo() {
        //$datas = $this->news_class_model->all();
        /* foreach($datas as $v){
          $data['sortname']=$v['sortname'];
          $data['id']=$v['id'];
          switch($v['sortname']){
          case "产品介绍":
          $data['url']=base_url($this->_site_path . "/news/goods_list/$v[id]");
          break;

          default:
          break;
          }
          } */
        $this->load->view_part($this->_site_path . '/common/header', $nav_data);
    }

    /**
     * 用js报错
     * @param string $value 要输出的值
     */
    public function error_js($value) {
        header("Content-type:text/html;charset=utf-8");
        echo "<script>alert('" . $value . "');history.go(-1)</script>";
    }

    /**
     * js提示消息盒子
     * @param type $value  提示的值
     * @param type $url  跳转的路径
     */
    public function message_js($value, $url) {

        header("Content-type:text/html;charset=utf-8");
        echo "<script>alert('" . $value . "');window.location.href='" . $url . "';</script>";
    }

    //判断用户是否已登陆
    protected function is_login() {

        $user_id = $this->session->userdata('user_id');
        $user_name = $this->session->userdata('user_name');
        $this->load->model('member_model');
        if ($user_id && $user_name) {
            //对比图片来源
            if (isset($_SERVER['HTTP_REFERER'])) {
                $url = parse_url($_SERVER['HTTP_REFERER']);
                if ($_SERVER['HTTP_HOST'] != $url["host"]) {
                    return false;
                }
            }
            $tmp_name = $this->member_model->get_value_by_pk($user_id, 'user_name');
            return $user_name == $tmp_name ? true : false;
        } else {
            return false;
        }
    }

}
